// pages/follow/follow.js
var util = require('../../utils/util.js');
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
Page({
  /**
   * 页面的初始数据
   */
  data: {
   datalist:[],
   url:'',
   check:'',
   nodata: true,
   page:1,
   limit:10,
   user_id:'',
   nodate:true,
   checktype:String 
  },
  //列表
  list: function (page, limit, user_id, more){
      var that = this;
      const data = {
        page: page,
        limit: limit,
        user_id: user_id
      }

      http.request({
        url: that.data.url,
        data: data,
        success: function (res) {
          wx.stopPullDownRefresh()
          var data = res.data.data
          for (var i = 0; i < data.length; i++) {
            data[i].created_at = util.timestampToTime(data[i].created_at);
          }
          
          if (!!more) {
            that.setData({
              datalist: that.data.datalist.concat(res.data.data),
              nodate: true
            })

            if (res.data.data.length == 0) {
              that.setData({
                nodata: false
              });
            }
          } else {
            that.setData({
              datalist: res.data.data,
              nodate: true
            })

            if (res.data.data.length == 0) {
              that.setData({
                nodate: false
              });
            }
          }
        }
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var title = ''; //顶部标题
    var url = ''; //访问api
    this.setData({
      user_id:options.id,
      checktype: options.name
    })
    if (options.name == 'follow'){
      title = '关注的人'
      this.setData({
        url:'mini/v1/personal/my_follow'
      })
    }else{
      title = '粉丝'
      this.setData({
        url: 'mini/v1/personal/follow_my'
      })
    }

    wx.setNavigationBarTitle({
      title: title//页面标题为路由参数
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.list(1, 10, this.data.user_id);
  },
  onPullDownRefresh() {
    this.list(1, 10, this.data.user_id);
  },
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    })
    this.list(this.data.page, this.data.limit, this.data.user_id, 'more');
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  follow_off:function(e){
    var id = e.currentTarget.dataset.id;
    var num = e.currentTarget.dataset.index;
    var that = this;
    if (that.data.checktype == 'follow') {
      wx.showModal({
        title: '提示',
        content: '确定不再关注？ ',
        success(res) {
          if (res.confirm) {
            http.request({
              url: 'mini/v1/release/follow_off',
              data: { id: id },
              methods: 'post',
              success: function (res) {
                var lists = that.data.datalist;
                  lists.splice(num, 1);
                  that.setData({
                    datalist: lists
                  });
              }
            })
          } else if (res.cancel) {
          }
        }
      })
    }else{
      if (e.currentTarget.dataset.checkid == 1) {
        wx.showModal({
          title: '提示',
          content: '确定不再关注？ ',
          success(res) {
            if (res.confirm) {
              http.request({
                url: 'mini/v1/release/follow_off',
                data: { id: id },
                methods: 'post',
                success: function (res) {
                  var lists = that.data.datalist;
                  that.setData({
                    [`datalist[${num}].is_each`]: '0'
                  });
                }
              })
            } else if (res.cancel) {
            }
          }
        })
      }else {
        http.request({
          url: 'mini/v1/release/follow_on',
          data: { id: id },
          methods: 'post',
          success: function (res) {
            var lists = that.data.datalist;
            that.setData({
              [`datalist[${num}].is_each`]: '1'
            })
          }
        })
      }
    }
  },
  //用户点击的
  is_follow:function(e){
    var id = e.currentTarget.dataset.id;
    var num = e.currentTarget.dataset.index;
    var that = this;
    if (e.currentTarget.dataset.checkid == 1) {
      wx.showModal({
        title: '提示',
        content: '确定不再关注？ ',
        success(res) {
          if (res.confirm) {
            http.request({
              url: 'mini/v1/release/follow_off',
              data: { id: id },
              methods: 'post',
              success: function (res) {
                var lists = that.data.datalist;
                that.setData({
                  [`datalist[${num}].is_each`]: '0'
                });
              }
            })
          } else if (res.cancel) {
          }
        }
      })
    } else {
      http.request({
        url: 'mini/v1/release/follow_on',
        data: { id: id },
        methods: 'post',
        success: function (res) {
          var lists = that.data.datalist;
          that.setData({
            [`datalist[${num}].is_each`]: '1'
          })
        }
      })
    }
  },
  //跳转详情
  stopcatch: function (e) {
    wx.navigateTo({
      url: '../personinformation/personinformation?id=' + e.currentTarget.dataset.id
    })
  }
})